package com.fish.anne.webapp.action;

import com.opensymphony.xwork2.Preparable;
import com.fish.anne.service.SystemAdminSessionManager;
import com.fish.anne.model.SystemAdminSession;
import com.fish.anne.webapp.action.BaseAction;

import java.util.List;

public class SystemAdminSessionAction extends BaseAction implements Preparable {
    private SystemAdminSessionManager systemAdminSessionManager;
    private List systemAdminSessions;
    private SystemAdminSession systemAdminSession;
    private String  sessId;

    public void setSystemAdminSessionManager(SystemAdminSessionManager systemAdminSessionManager) {
        this.systemAdminSessionManager = systemAdminSessionManager;
    }

    public List getSystemAdminSessions() {
        return systemAdminSessions;
    }

    /**
     * Grab the entity from the database before populating with request parameters
     */
    public void prepare() {
        if (getRequest().getMethod().equalsIgnoreCase("post")) {
            // prevent failures on new
            String systemAdminSessionId = getRequest().getParameter("systemAdminSession.sessId");
            if (systemAdminSessionId != null && !systemAdminSessionId.equals("")) {
                systemAdminSession = systemAdminSessionManager.get(new String(systemAdminSessionId));
            }
        }
    }

    public String list() {
        systemAdminSessions = systemAdminSessionManager.getAll();
        return SUCCESS;
    }

    public void setSessId(String  sessId) {
        this. sessId =  sessId;
    }

    public SystemAdminSession getSystemAdminSession() {
        return systemAdminSession;
    }

    public void setSystemAdminSession(SystemAdminSession systemAdminSession) {
        this.systemAdminSession = systemAdminSession;
    }

    public String delete() {
        systemAdminSessionManager.remove(systemAdminSession.getSessId());
        saveMessage(getText("systemAdminSession.deleted"));

        return SUCCESS;
    }

    public String edit() {
        if (sessId != null) {
            systemAdminSession = systemAdminSessionManager.get(sessId);
        } else {
            systemAdminSession = new SystemAdminSession();
        }

        return SUCCESS;
    }

    public String save() throws Exception {
        if (cancel != null) {
            return "cancel";
        }

        if (delete != null) {
            return delete();
        }

        boolean isNew = (systemAdminSession.getSessId() == null);

        systemAdminSessionManager.save(systemAdminSession);

        String key = (isNew) ? "systemAdminSession.added" : "systemAdminSession.updated";
        saveMessage(getText(key));

        if (!isNew) {
            return INPUT;
        } else {
            return SUCCESS;
        }
    }
}